#Requires -Version 3
#Requires -RunAsAdministrator

<#
.SYNOPSIS 
Hydration Script

.DESCRIPTION
Hydration Environment for MDT

.NOTES
Copyright Keith Garner (KeithGa@DeploymentLive.com), All rights reserved.

.LINK
http://MDTEx.CodePlex.com

#>

[CmdletBinding()]
param(
    [switch] $SuperQuick,
    [switch] $CleanOnly,
    [string] $ScriptVars = "$PSScriptRoot\ScriptBlock.xml"
)

#region Environment Setup

"##########################################################" | Write-Verbose
"##  Start Hydration Full Test Regression                  " | Write-Verbose
"##########################################################" | Write-Verbose

Write-Verbose "Starting $PSScriptRoot\FullTest(Host).ps1"

$ErrorActionPreference  = "Stop" 
set-strictmode -version Latest

if ( Get-Module MDTEx ) { Remove-Module MDTEx }
if ( get-module MicrosoftDeploymentToolkit ) { Remove-Module MicrosoftDeploymentToolkit }

write-verbose "Load Library Modules $PSScriptRoot\Module\MDTEx"
Import-Module "$PSScriptRoot\Module\MDTEx" -ArgumentList ($PSBoundParameters['Verbose'] -eq $true) | out-string | write-verbose

Remove-Item $PSScriptRoot\*.log -ErrorAction SilentlyContinue
if (-not (test-path Variable:logpath)) { set-LogPath "$PSScriptRoot\Hydrate.log" }

$ScriptXML = Import-Clixml $ScriptVars
$scriptXML | out-string | Write-Verbose

$MDTExPath = "${env:programfiles(x86)}\MDT 2013 Extensions"

#endregion

#region Cleanup Host 
"##########################################################" | Write-Verbose
"##  Cleanup of Test Host                                  " | Write-Verbose
"##########################################################" | Write-Verbose

"Stop Running VM's " | Write-Verbose
if ( Test-Path "$($scriptxml.DeploymentLocalPath)\Control\*.cre")
{
    $VMList = get-childitem -path "$($scriptxml.DeploymentLocalPath)\Control\*.cre" -directory | %{ $_.Name.Replace(".CRE","") } | get-vm -erroraction SilentlyContinue | ?{ $_.State -ne 'Off' }
    $vmList | stop-VM -Force -TurnOff
    if ($VMList) { Start-Sleep 10 }
}

if (gwmi win32_share -filter "name='$($scriptXML.DeploymentNetShare)'")
{
    "Remove Deployment share to ensure no open files " |Write-Verbose
    'Y' | net.exe share /Del $scriptXML.DeploymentNetShare| out-string |Write-Verbose
}

if ( test-path $scriptXML.DeploymentLocalPath )
{
    "remove all Junction points from DeploymentShare" | Write-Verbose
    get-childitem -path $scriptXML.DeploymentLocalPath -recurse -Attributes ReparsePoint | %{ $_.Delete() }
    "Remove local Deployment Directory" |Write-Verbose
    Remove-Item -Recurse -Force $scriptXML.DeploymentLocalPath
}

if ( -not $SuperQuick )
{

    $ExistingMDTEx = gwmi win32_product -filter "Name = 'MicrosoftDeploymentToolkit2013_Extensions'"
    if ( $ExistingMDTEx )
    {
        "remove MSIPackage: $($ExistingMDTEx.IdentifyingNumber)" | Write-Verbose
        & msiexec.exe /qb /x $($ExistingMDTEx.IdentifyingNumber) | out-null
    }

    if ( Test-Path $MDTExPath ) {Remove-Item -Recurse -Force $MDTExPath }

    if ( $CleanOnly ) { exit }

    "##########################################################" | Write-Verbose
    "##  Install MSI 1                                         " | Write-Verbose
    "##########################################################" | Write-Verbose

    if ( Test-Path $PSScriptRoot\MDTEX_MSI.log ) { Remove-Item $PSScriptRoot\MDTEX_MSI.log }
    & msiexec.exe /qb- /i $PSScriptRoot\MicrosoftDeploymentToolkit2013_Extensions.msi /l*v $PSScriptRoot\MDTEX_MSI.log | out-null

    if ( -not (Test-Path "${env:programfiles(x86)}\MDT 2013 Extensions" ) )
    {
        throw "MDTEX Product is not installed"
    }

}

#endregion

#region Run Hydration Script

"##########################################################" | Write-Verbose
"##  Run Hydration Script                                  " | Write-Verbose
"##########################################################" | Write-Verbose

$scriptXML.HydrationScripts = get-childitem -recurse -path $MDTExPath\hydrate -Include "*.mdt.ps1" 
& $MDTExPath\hydrate\Hydrate.ps1 -ConfigBlock $scriptXML

#endregion

"##########################################################" | Write-Verbose
"##  Done                                                  " | Write-Verbose
"##########################################################" | Write-Verbose

